using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UPanel : Panel
    {
        private int cornerRadius = 10;

        [Category("外观")]
        [Description("圆角半径")]
        public int CornerRadius
        {
            get { return cornerRadius; }
            set 
            {
                cornerRadius = value;
                this.Invalidate();
            }
        }

        private Color borderColor = Color.Black;

        [Category("外观")]
        [Description("边框颜色")]
        public Color BorderColor
        {
            get { return borderColor; }
            set 
            {
                borderColor = value;
                this.Invalidate();
            }
        }

        private int borderWidth = 1;

        [Category("外观")]
        [Description("边框宽度")]
        public int BorderWidth
        {
            get { return borderWidth; }
            set 
            {
                borderWidth = value;
                this.Invalidate();
            }
        }

        public UPanel()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 创建圆角矩形路径
            Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            GraphicsPath path = GetRoundRectangle(rect, cornerRadius);

            // 填充背景
            using (Brush brush = new SolidBrush(this.BackColor))
            {
                g.FillPath(brush, path);
            }

            // 绘制边框
            using (Pen pen = new Pen(borderColor, borderWidth))
            {
                g.DrawPath(pen, path);
            }
        }

        private GraphicsPath GetRoundRectangle(Rectangle rect, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            int diameter = radius * 2;

            // 左上圆角
            path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
            // 右上圆角
            path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
            // 右下圆角
            path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
            // 左下圆角
            path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
            path.CloseFigure();

            return path;
        }
    }
}